import socket
import json

# con = json.loads(content)
HOST = '127.0.0.1'  # or 'localhost'
PORT = 21563
BUFSIZ = 1024
ADDR = (HOST, PORT)
user_data = {}


tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpCliSock.connect(ADDR)

def pairing():
    data={"command":"pairing","num":user_data["num"]}
    tcpCliSock.send(json.dumps(data, ensure_ascii=False).encode())

def input_data():
    stu_name = input('>请输入姓名')
    stu_num = input('>请输入学号')
    stu_sex = input('>请输入性别')
    stu_bir = input('>请输入生日')
    stu_local = input('>请输入籍贯')
    stu_work = input('>请输入行业')
    stu_hobbit = input('>请输入性格')
    stu_prov = input('>请输入所在地')
    # data = str(data)
    #      lis = [{},{},{}...]
    # 数据转json格式


    user_data["name"] = stu_name
    user_data["num"] = stu_num
    user_data["sex"] = stu_sex
    user_data["bir"] = stu_bir
    user_data["local"] = stu_local
    user_data["work"] = stu_work
    user_data["hobbit"] = stu_hobbit
    user_data["prov"] = stu_prov
    # 剪切时间格式数据
    #      user_data["time"] = str(data[2]).replace("datetime.datetime", "")
    #      result.append(user_data)
    print(user_data)

def upload_data():
    command_data={"command":"upload_data","num":user_data["num"],"data":user_data}
    str_json = json.dumps(command_data, ensure_ascii=False)  # 将python中的字典转换为json字符串
    print(str_json)
    
    tcpCliSock.send(str_json.encode())
#输入数据    
input_data()
upload_data()
#开始匹配
while True:
    stu_prov = input('>是否匹配')
    pairing()
    #接收结果
    str_json = tcpCliSock.recv(BUFSIZ)
    print(str_json.decode('utf-8'))

tcpCliSock.close()